package com.zahb.qadx.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.CommonResponse;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.EditTextHelper;
import com.zahb.qadx.util.MD5Util;
import com.zahb.qadx.util.RegularUtil;
import com.zahb.qadx.util.SimpleTextWatcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by huwei.
 * Data 2020-10-13.
 * Time 11:03.
 */
public class UpdatePwdActivity extends BaseActivity {
    @BindView(R.id.et_old_pwd)
    AppCompatEditText etOldPwd;
    @BindView(R.id.et_new_pwd)
    AppCompatEditText etNewPwd;
    @BindView(R.id.et_again_pwd)
    AppCompatEditText etAgainPwd;
    @BindView(R.id.btn_save)
    AppCompatButton btnSave;
    String oldPwd, newPwd;

    @Override
    protected int getLayout() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        mTopBarContainer.setVisibility(View.VISIBLE);
        EditTextHelper.getEditTextDeleteIconWrapper(etOldPwd).setTextWatcher(mSimpleTextWatcher);
        EditTextHelper.getEditTextDeleteIconWrapper(etNewPwd).setTextWatcher(mSimpleTextWatcher);
        EditTextHelper.getEditTextDeleteIconWrapper(etAgainPwd).setTextWatcher(mSimpleTextWatcher);
    }
    private SimpleTextWatcher mSimpleTextWatcher = new SimpleTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            btnSave.setEnabled(!CompatHelper.isEmpty(etOldPwd) && !CompatHelper.isEmpty(etNewPwd) && !CompatHelper.isEmpty(etAgainPwd));
        }
    };
    @Override
    protected int getTitleStringRes() {
        return R.string.update_pwd;
    }

    @OnClick({R.id.btn_save})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn_save) {
            assert etOldPwd.getText() != null;
            assert etNewPwd.getText() != null;
            assert etAgainPwd.getText() != null;
            if (TextUtils.isEmpty(etOldPwd.getText().toString().trim())) {
                showBindToast("请输入原密码");
                return;
            }
            if (TextUtils.isEmpty(etNewPwd.getText().toString().trim())) {
                showBindToast("请输入新密码");
                return;
            }
            if (!RegularUtil.isValidPassword(etNewPwd.getText().toString().trim())) {
                showBindToast("新密码格式不正确");
                return;
            }
            if (TextUtils.isEmpty(etAgainPwd.getText().toString().trim())) {
                showBindToast("请输入确认密码");
                return;
            }
            if (!RegularUtil.isValidPassword(etAgainPwd.getText().toString().trim())) {
                showBindToast("确认密码格式不正确");
                return;
            }
            oldPwd = etOldPwd.getText().toString().trim();
            newPwd = etNewPwd.getText().toString().trim();
            if (!newPwd.equals(etAgainPwd.getText().toString().trim())) {
                showBindToast("新密码和确认密码不一致");
                return;
            }
            updatePwd(etOldPwd.getText().toString().trim(), etNewPwd.getText().toString().trim());
        }
    }

    private void updatePwd(String oldPwd, String newPwd) {
        String oldMd5Pwd = MD5Util.stringToMD5(oldPwd);
        String newMd5Pwd = MD5Util.stringToMD5(newPwd);
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getUpdatePwd(oldMd5Pwd, newMd5Pwd)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        getUpData(response);
                    }, throwable -> {
                        throwable.printStackTrace();
                        hideProgressDialog();
                    }));
        }
    }

    private void getUpData(CommonResponse<Object> response) {
        if (response.getStatusCode() == 200) {
            showBindToast("密码修改成功");
            finish();
        } else {
            showBindToast(response.getErrorInfo());
        }
    }
}
